package com.itcast.beans.ioc.xml;

import com.itcast.beans.ioc.BeanDefinition;
import com.itcast.beans.ioc.DefaultListableBeanFactory;
import com.itcast.beans.ioc.Property;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 读取xml内容
 * 接续所有bean标签
 * 将bean标签封装为BeanDefinition
 * 注册到容器中
 * @作者 itcast
 * @创建日期 2020/6/18 14:29
 **/
public class XmlBeanDefinitionReader {
    // 对应的bean工厂核心对象
    private final DefaultListableBeanFactory beanFactory;
    public XmlBeanDefinitionReader(DefaultListableBeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    /**
     * @param config 配置文件名称
     */
    public void loadBeanDefinitions(String config){
        InputStream resource = this.getClass().getClassLoader().getResourceAsStream(config);
        doLoadBeanDefinitions(resource);
    }
    /**
     * 解析xml
     * 封装BeDefinition
     * 注册到容器
     * @param inputStream
     */
    public void doLoadBeanDefinitions(InputStream inputStream){
        // 解析xml为Document文档
        Document doc = doLoadDocument(inputStream);
        registryBeanDefinitions(doc);
        System.out.println(111);
    }
    /**
     * 将指定xml文件解析为Document文档对象
     * @param inputStream
     * @return
     */
    public Document doLoadDocument(InputStream inputStream){
        SAXReader saxReader = new SAXReader();
        try {
            return saxReader.read(inputStream);
        } catch (DocumentException e) {
            e.printStackTrace();
            System.out.println("解析xml配置文件失败,请检查配置文件格式");
        }
        return null;
    }

    /**
     * 解析Document文档中所有的bean标签
     * 每个bean标签封装成一个BeanDefinition对象
     * 将BeanDefinition对象注册到容器
     * @param doc
     */
    public void registryBeanDefinitions(Document doc){
        // 获取该文档中所有 bean标签
        List<Element> list = doc.selectNodes("//bean");
        if(list!=null&&list.size()>0){
            // 遍历所有的bean标签
            for (Element beanElement : list) {
                // 将每个bean标签配置的信息封装成一个beanDefinition对象
                BeanDefinition beanDefinition = new BeanDefinition();
                String beanName = beanElement.attributeValue("id");
                beanDefinition.setBeanName(beanName);
                beanDefinition.setClassName(beanElement.attributeValue("class"));
                String scope = beanElement.attributeValue("scope");
                if(!"".equals(scope)){
                    beanDefinition.setScope(scope);
                }
                // 获取该bean标签下所有properties标签下属性信息
                List<Element> propertyList = beanElement.elements("property");
                List<Property> properties = new ArrayList<Property>();
                if(propertyList!=null&&propertyList.size()>0){
                    for (Element propertyElement : propertyList) {
                        Property property = new Property();
                        property.setName(propertyElement.attributeValue("name"));
                        property.setRef(propertyElement.attributeValue("ref"));
                        property.setValue(propertyElement.attributeValue("value"));
                        properties.add(property);
                    }
                }
                beanDefinition.setProperties(properties);
                // 注册到容器
                beanFactory.registerBeanDefinition(beanName,beanDefinition);
            }
        }
    }
}
